﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace wsn
{
    //数据库基本类
    public class BaseDB
    {
        private Message mes;
        private bool is_open;
        private MySqlConnection connect;
        private MySqlCommand command;
        public Message Mes
        {
            get { return this.mes; }
            set { this.mes = value; }
        }
        public bool IsOpen
        {
            get { return this.is_open; }
            set { }
        }
        public BaseDB(Message message)
        {
            this.mes = message;
            this.is_open = false;
            this.connect = new MySqlConnection();
            this.command = new MySqlCommand();
        }
        public BaseDB(BaseDB obj)
        {
            this.mes = obj.mes;
            this.is_open = obj.is_open;
            this.connect = obj.connect;
            this.command = obj.command;
        }
        public void OpenDatabase()
        {
            this.connect.ConnectionString = this.mes.GetString();
            try
            {
                this.connect.Open();
                this.is_open = true;
            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void ExeNoneQuery(string Query)
        {
            try
            {
                if (!this.is_open)
                {
                    try
                    {
                        this.OpenDatabase();
                    }
                    catch (MySqlException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                this.command.Connection = this.connect;
                this.command.CommandText = Query;
                this.command.ExecuteNonQuery();
            }
            catch (MySqlException excep)
            {
                throw excep;
            }
        }
        public void SetCommandText(string query)
        {
            this.command.CommandText = query;
        }
        public MySqlDataReader ExeQuery(string Query)
        {
            MySqlDataReader reader;
            try
            {
                if (!this.is_open)
                {
                    try
                    {
                        this.OpenDatabase();
                    }
                    catch (MySqlException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                this.command.Connection = this.connect;
                this.command.CommandText = Query;
                reader = this.command.ExecuteReader();
                return reader;
            }
            catch (MySqlException excep)
            {
                throw excep;
            }
        }
        public void CloseDatabase()
        {
            this.connect.Close();
            this.is_open = false;
        }
    }      
}
    
